#coding:utf-8
import os
import hashlib
from django.db import models
from django.utils import timezone
from django.core.cache import cache
from lovewith.album.models import Album
from lovewith.api.qiniu import Qiniu
from lovewith.utils.mtcache import MtCache
from lovewith.website.models import Category


#类目推荐关键词
class PromoteCateTag(models.Model):
    tag = models.CharField(max_length=255, verbose_name=u'关键词')
    link = models.CharField(max_length=255, verbose_name=u'连接')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')
    cate = models.ForeignKey(Category, verbose_name=u'分类')

    class Meta:
        verbose_name_plural = u'类目推荐标签'
        db_table = 'mt_promote_cate_tag'

    def save(self):
        super(PromoteCateTag, self).save()
        #清空缓存
        cache.delete('cache_promote_cate_tag_%s' % self.cate.id)


def get_promote_upload_path(instance, filename):
    file_ext = os.path.splitext(filename)[1]
    new_name = hashlib.new('md5', filename).hexdigest()
    return 'upload.tmp/%s%s' % (new_name, file_ext)

#首页推荐位
topic_type = ((4, u'610x410轮播图'), (5, u'290x70广告'))


class PromoteHomeCycle(models.Model):
    banner = models.ImageField(upload_to=get_promote_upload_path, max_length=255, verbose_name=u'图片')
    title = models.CharField(max_length=255, blank=True, verbose_name=u'标题')
    link = models.CharField(max_length=255, verbose_name=u'链接')
    description = models.CharField(max_length=255, blank=True, verbose_name=u'描述')
    type = models.SmallIntegerField(choices=topic_type, max_length=1, verbose_name=u'类型')

    class Meta:
        verbose_name_plural = u'[首页]轮播图&&广告'
        db_table = 'mt_promote_home_cycle'

    def save(self):
        super(PromoteHomeCycle, self).save()

        #保存图片到Qiniu
        if self.banner.path.find('http') < 0:
            promote_banner = Qiniu('mt-other').upload(self.banner.path)
            if promote_banner:
                self.banner = promote_banner['path']
                super(PromoteHomeCycle, self).save()
        #更新缓存
        MtCache('home_cycle_new').delete()

    def delete(self):
        super(PromoteHomeCycle, self).delete()
        #更新缓存
        MtCache('home_cycle_new').delete()


#商品页推荐标签
class PromoteGoodsCommandTag(models.Model):
    tags = models.TextField(blank=True, verbose_name=u'标签')

    def __unicode__(self):
        return self.tags

    class Meta:
        verbose_name_plural = u'商品页推荐标签'
        db_table = 'mt_goods_tag'


class PromoteHomeChoice(models.Model):
    title = models.CharField(max_length=255, verbose_name=u'标题')
    content = models.TextField(blank=True, verbose_name=u'标签')
    images_ids = models.CharField(max_length=255, verbose_name=u'图片列表')
    post_date = models.DateTimeField(default=timezone.now, verbose_name=u'时间')

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name_plural = u'[首页]每日精选'
        db_table = 'mt_promote_home_choice'

    def save(self):
        super(PromoteHomeChoice, self).save()
        self.images_ids = self.images_ids.replace('，', ',')
        super(PromoteHomeChoice, self).save()
        #删除缓存
        MtCache('home_choice').delete(end=1)
        MtCache('home_choice').delete(end=10)


#首页推荐灵感板
class PromoteHomeAlbum(models.Model):
    album = models.ForeignKey(Album, verbose_name=u'灵感板')

    def __unicode__(self):
        return self.album.name

    class Meta:
        verbose_name_plural = u'[首页]推荐灵感板'
        db_table = 'mt_promote_home_album'

    def save(self):
        super(PromoteHomeAlbum, self).save()

        #更新缓存
        MtCache('home_album').delete()


#推荐订阅
class CommandSubscribe(models.Model):
    keyword = models.CharField(max_length=255, verbose_name=u'标签')
    banner = models.ImageField(upload_to=get_promote_upload_path, max_length=255, verbose_name=u'图片')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')

    def __unicode__(self):
        return u'订阅词'

    def save(self):
        super(CommandSubscribe, self).save()

        #保存图片到upyun
        if self.banner.path.find('http') < 0:
            promote_banner = Qiniu('mt-other').upload(self.banner.path)
            if promote_banner:
                self.banner = promote_banner['path']
                super(CommandSubscribe, self).save()

    class Meta:
        verbose_name_plural = u'推荐订阅'
        db_table = 'mt_subscribe_command'


#首页栏目
class PromoteHomeColumn(models.Model):
    title = models.CharField(max_length=255, verbose_name=u'标题')
    content = models.TextField(verbose_name=u'内容')
    sort = models.SmallIntegerField(default=0, verbose_name=u'排序')

    class Meta:
        verbose_name_plural = u'[首页]栏目'
        db_table = 'mt_promote_home_column'